package com.pig4cloud.pig.demotwo.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.PersonEntity;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.demotwo.domain.Players;
import com.pig4cloud.pig.demotwo.feign.RemotePersonService;
import com.pig4cloud.pig.demotwo.service.PlayersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 
 *
 * @author pig
 * @date 2025-07-22 13:46:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/players" )
@Tag(description = "players" , name = "管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PlayersController {

    private final  PlayersService playersService;
    @Autowired
    private RemotePersonService remotePersonService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param players 
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pig_players_view')" )
    public R getPlayersPage(@ParameterObject Page page, @ParameterObject PersonEntity person) {
        LambdaQueryWrapper<Players> wrapper = Wrappers.lambdaQuery();
        System.out.println("远程调用输出" + remotePersonService.getById(1947213456149901314L));
        return R.ok(playersService.page(page, wrapper));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('pig_players_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(playersService.getById(id));
    }

    /**
     * 新增
     * @param players 
     * @return R
     */
    @Operation(summary = "新增" , description = "新增" )
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pig_players_add')" )
    public R save(@RequestBody Players players) {
        return R.ok(playersService.save(players));
    }

    /**
     * 修改
     * @param players 
     * @return R
     */
    @Operation(summary = "修改" , description = "修改" )
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pig_players_edit')" )
    public R updateById(@RequestBody Players players) {
        return R.ok(playersService.updateById(players));
    }

    /**
     * 通过id删除
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除" , description = "通过id删除" )
    @SysLog("通过id删除" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pig_players_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(playersService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param players 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pig_players_export')" )
    public List<Players> export(Players players,Long[] ids) {
        return playersService.list(Wrappers.lambdaQuery(players).in(ArrayUtil.isNotEmpty(ids), Players::getId, ids));
    }
}